package com.api.gbhg.liuwenbo.version3.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.liuwenbo.version3.pojo.BudgetSectionIncome3;
import com.api.gbhg.liuwenbo.version3.util.ApiUtil;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.general.GCONST;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description lwb
 * @Author 非财政拨款收入预算统计表-经费科报表
 * @Veresion 1.0
 * @Date 2023.02.04
 */
@Path("v3/NoFinancialInStatApi")
public class NoFinancialInStatApi {
    private static LogTool logTool = new LogTool("/log/gbhg/report/NoFinancialInStatApi",false);
    private DateTools dateTools = new DateTools();
    @POST
    @Path("/getNoFinancialInStatList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getNoFinancialInStatList(Map param){
        Map msgMap = new HashMap();
        logTool.writeLog("接收参数："+ JSONObject.toJSONString(param));
        if (param == null ||!param.containsKey("nf")){
            msgMap.put("code","400");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        try {
            String shrq = "";
            if(!param.containsKey("shrq")){
                shrq = dateTools.getCurrentDate();
            }
            if(param.containsKey("shrq")){
                if(param.get("shrq").toString().isEmpty()){
                    shrq = dateTools.getCurrentDate();
                }else{
                    shrq = param.get("shrq").toString();
                }
            }

            List<BudgetSectionIncome3> listData = getData(param.get("nf").toString(),shrq);

            msgMap.put("code",200);
            msgMap.put("msg","success");
            msgMap.put("data",listData);
        } catch (Exception e) {
            e.printStackTrace();
            msgMap.put("code",500);
            msgMap.put("msg",e.toString());
            msgMap.put("data","");
        }

        return JSON.toJSONString(msgMap);
    }

    /**
     * 获取数据
     * @param nf
     * @return
     */
    public List<BudgetSectionIncome3> getData(String nf,String shrq ){
        RecordSet rs = new RecordSet();
        //因只需要查拱北 所以带单位条件
        String sql = "select sr.*,fl.mc from uf_fzysr sr left join uf_srysfl fl on sr.szfl = fl.id where sr.dw = 1 and sr.nf = "+nf+" and fl.id=7";
        rs.executeQuery(sql);
        List<BudgetSectionIncome3> list = new LinkedList<>();
        while (rs.next()){
            String flmc = rs.getString("mc");
            String flid = rs.getString("yssrfl");
            list.addAll(getDataXm(rs.getString("id"),flmc,flid,nf,shrq));
        }
        return list;
    }

    /**
     * 获取二级数据 实际跟下级数据同表只不过查二级条件不同
     * @param id
     * @return
     */
    public List<BudgetSectionIncome3> getDataXm(String id,String flmc,String flid,String nf,String shrq){
        RecordSet rs = new RecordSet();
        List<BudgetSectionIncome3> list = new LinkedList<>();
        String sql = "select * from uf_fzysrxm where szfl = "+id+" ";
        //2023年10月10日 增加对非项目支出的判断
        if(!flid.equals("5") && !flid.equals("8")){
            sql = " SELECT * FROM uf_fzysr WHERE dw=1 AND nf="+nf+" AND szfl="+flid;
        }
        rs.executeQuery(sql);

        while (rs.next()){
            BudgetSectionIncome3 bean = new BudgetSectionIncome3();
            bean.setCategory(flid);
            bean.setId(rs.getString("id"));
            bean.setName(flmc);
            bean.setXmmc( (!flid.equals("5") && !flid.equals("8")) ? flmc : rs.getString("xmmc"));
            bean.setXmdm((!flid.equals("5") && !flid.equals("8")) ? "" : rs.getString("xmdm"));
            bean.setSjsr(rs.getString("sjsr"));
            bean.setYqndsjjz(rs.getString("yqndsjjzje"));
            bean.setYjsr(rs.getString("yjsr"));
            String snhp = rs.getString("snjzhpje");
            String bnhp = rs.getString("yhpje");
            //2023年4月11日 huangwm 非财政拨款的支出金额查询不用添加kmjxj
            bean.setSnzc(ApiUtil.getDfcz_tybxje("1","1",rs.getString("id"),"1",nf,shrq, flid));
            bean.setBnzc(ApiUtil.getDfcz_tybxje("1","0",rs.getString("id"),"1",nf,shrq,flid));
            bean.setSnyssy(new BigDecimal(snhp).subtract(new BigDecimal(bean.getSnzc())).toString());
            bean.setBnyssy(new BigDecimal(bnhp).subtract(new BigDecimal(bean.getBnzc())).toString());
            BigDecimal hpze = new BigDecimal(snhp).add(new BigDecimal(bnhp));//核批总额
            bean.setHpze(hpze.toString());
            BigDecimal zcze = new BigDecimal(bean.getSnzc()).add(new BigDecimal(bean.getBnzc()));//支出总额
            bean.setZcze(zcze.toString());
            BigDecimal bai = new BigDecimal("100.00");
            if (hpze.compareTo(BigDecimal.ZERO)>0 && zcze.compareTo(BigDecimal.ZERO)>0){
                //huangwm 2023年8月29日 修复执行率显示不正确 .multiply(bai)
                //bean.setZxl(zcze.divide(hpze, 3).multiply(bai).setScale(2,BigDecimal.ROUND_HALF_UP).toString()+"%");
                bean.setZxl(zcze.divide(hpze,4,BigDecimal.ROUND_HALF_UP).multiply(bai).setScale(2).toString()+"%");
            }else{
                bean.setZxl("0.00%");
            }
            list.add(bean);
        }

        return list;
    }

    public static void main(String[] args) {
        String rootPath = "D:\\WEAVER\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");

        NoFinancialInStatApi api = new NoFinancialInStatApi();
        Map param = new HashMap();
        param.put("nf","2023");
        param.put("shrq","");
        System.err.println(api.getNoFinancialInStatList(param));
//        BigDecimal hpze = new BigDecimal("68338861.15");//核批总额
//        BigDecimal zcze = new BigDecimal("53158633.14");//核批总额
//        BigDecimal bai = new BigDecimal("100.00");
//        System.err.println(zcze.divide(hpze,4,BigDecimal.ROUND_HALF_UP).multiply(bai).setScale(2).toString()+"%");
    }
}
